﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpenseSystem.Model;
using ExpenseSystem.Fether;
using ExpenseSystem.DataUpdater;
using ExpenseSystem.Common;
using ExpenseSystem.DataUpdater.Validation;

namespace ExpenseSystem.Core.QuoteModules
{
    public class QuoteUpdaterRegister
    {
        int rows;
        public NormalDataUpdater<QuoteModel> GetQuoteUpdater()
        {
            NormalDataUpdater<QuoteModel> systemDataUpdater = new NormalDataUpdater<QuoteModel>();
            systemDataUpdater.DataUpdating += DataNotSpaceValid;
            systemDataUpdater.DataUpdating += SingletonValid;
            systemDataUpdater.DataUpdating += StateValid;
            systemDataUpdater.DataDeleting += CheckAvaliable;
            return systemDataUpdater;
        }
        private void CheckAvaliable(object sender, DataValidationEventArgs<QuoteModel> e)
        {
            if(e.Delete.State != Definition.STATE_NORMAL)
            {
                e.Message = "只有在未提交的单据情况下才可删除";
                e.Cancle = true;
                return;
            }
            string message;
            TitleQuoteFetcher titleQuoteFetcher = new TitleQuoteFetcher();
            QuoteDetailFetcher quoteDetailFetcher = new QuoteDetailFetcher();
            RegionQuoteFetcher regionQuoteFetcher = new RegionQuoteFetcher();
            SalesPointQuoteFetcher salesPointQuoteFetcher = new SalesPointQuoteFetcher();
            OrderDetailFetcher orderFetcher = new OrderDetailFetcher();
            if((titleQuoteFetcher.ListByMainId(e.Delete.Site, e.Delete.AutoId, out message, out rows).Count +
                regionQuoteFetcher.ListByMainId(e.Delete.Site, e.Delete.AutoId, out message, out rows).Count +
                salesPointQuoteFetcher.ListByMainId(e.Delete.Site, e.Delete.AutoId, out message, out rows).Count+
                quoteDetailFetcher.ListByMainId(e.Delete.Site,e.Delete.AutoId,out message, out rows).Count+
                orderFetcher.ListByQuote(e.Delete.Site,e.Delete.AutoId,out message, out rows).Count)>0
                )
            {
                e.Message = "报价单已被占用，不能删除";
                e.Cancle = true;
                return;
            }
        }
        private void StateValid(object sender,DataValidationEventArgs<QuoteModel> e)
        {
            string message;
            TitleQuoteFetcher titleQuoteFetcher = new TitleQuoteFetcher();
            QuoteDetailFetcher quoteDetailFetcher = new QuoteDetailFetcher();
            RegionQuoteFetcher regionQuoteFetcher = new RegionQuoteFetcher();
            SalesPointQuoteFetcher salesPointQuoteFetcher = new SalesPointQuoteFetcher();
            if (e.Insert.State != Definition.STATE_NORMAL)
            {
                if (e.Insert.PropertyUpdated.Contains("Code") ||
                    e.Insert.PropertyUpdated.Contains("Name") ||
                    e.Insert.PropertyUpdated.Contains("OpenDate") ||
                    e.Insert.PropertyUpdated.Contains("DueDate") ||
                    e.Insert.PropertyUpdated.Contains("Global") ||
                    e.Insert.PropertyUpdated.Contains("Lv"))
                {
                    e.Message = "已审核的表单不能修改";
                    e.Cancle = true;
                    return;
                }
                if(quoteDetailFetcher.ListByMainId(e.Insert.Site, e.Insert.AutoId,out message, out rows).Count==0)
                {
                    e.Message = "审核需要有明细项";
                    e.Cancle = true;
                    return;
                }
                if(!e.Insert.Global&&(titleQuoteFetcher.ListByMainId(e.Insert.Site, e.Insert.AutoId, out message, out rows).Count +
                    regionQuoteFetcher.ListByMainId(e.Insert.Site, e.Insert.AutoId, out message, out rows).Count  +
                    salesPointQuoteFetcher.ListByMainId(e.Insert.Site, e.Insert.AutoId, out message, out rows).Count)==0)
                {
                    e.Message = "非全局报价单必须有下发到区域、岗位或门店";
                    e.Cancle = true;
                    return;
                }
            }
            if(e.Insert.State == Definition.STATE_NORMAL)
            {
                OrderDetailFetcher orderFetcher = new OrderDetailFetcher();
                if(orderFetcher.ListByQuote(e.Insert.Site,e.Insert.AutoId,out message, out rows).Count>0)
                {
                    e.Message = "非草稿状态下不允许存在下游单据";
                    e.Cancle = true;
                    return;
                }
            }
        }
        private void SingletonValid(object sender, DataValidationEventArgs<QuoteModel> e)
        {
            QuoteFetcher QuoteFetcher = new QuoteFetcher();
            string message = "";
            QuoteModel Quote = QuoteFetcher.GetByCode( e.Insert.Site, e.Insert.Code, out message);
            if (Quote != null && Quote.AutoId != e.Insert.AutoId)
            {
                e.Cancle = true;
                message = String.Format("代号[{0}]已被占用", Quote.Code);
                return;
            }
        }

        private void DataNotSpaceValid(object sender, DataValidationEventArgs<QuoteModel> e)
        {
            if (String.IsNullOrWhiteSpace(e.Insert.Code))
            {
                e.Message = "代号不能为空";
                e.Cancle = true;
                return;
            }
            if (String.IsNullOrWhiteSpace(e.Insert.Name))
            {
                e.Message = "名称不能为空";
                e.Cancle = true;
                return;
            }
            DateTime dt1, dt2;
            dt1 = dt2 = DateTime.Now;
            DateTime.TryParse(e.Insert.OpenDate, out dt1);
            DateTime.TryParse(e.Insert.DueDate, out dt2);
            if(dt1>=dt2)
            {
                e.Message = "结束时间必须大于开始时间";
                e.Cancle = true;
                return;
            }
        }
    }
}
